package pl.enigmatic.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import pl.enigmatic.HasName;

/**
 * @author reggie_7
 */
public class Triplex<T> implements Iterable<T> {

	private final T left;
	private final T middle;
	private final T right;
	private final List<T> list = new ArrayList<T>(3);

	public Triplex(T left, T middle, T right) {
		this.left = left;
		list.add(left);
		this.middle = middle;
		list.add(middle);
		this.right = right;
		list.add(right);
	}

	public T getLeft() {
		return left;
	}

	public T getMiddle() {
		return middle;
	}

	public T getRight() {
		return right;
	}

	public Iterator<T> iterator() {
		return new Iterator<T>() {

			private final Iterator<T> base = list.iterator();

			public boolean hasNext() {
				return base.hasNext();
			}

			public T next() {
				return base.next();
			}

			public void remove() {
			}
		};
	}

	public static class Builder<T> {

		protected T left;
		protected T middle;
		protected T right;

		public T setLeft(T t) {
			this.left = t;
			return t;
		}

		public T setMiddle(T t) {
			this.middle = t;
			return t;
		}

		public T setRight(T t) {
			this.right = t;
			return t;
		}

		public Triplex<T> build() {
			return new Triplex<T>(left, middle, right);
		}
	}

	public static class HasNameBuilder<T extends HasName> extends Builder<T> {

		public HasNameBuilder<T> rename(String prefix) {
			left.setName(prefix + ".left");
			middle.setName(prefix + ".middle");
			right.setName(prefix + ".right");
			return this;
		}
	}
}
